package com.hhsjdesign.dynamicui.utils

import com.hhsjdesign.dynamicui.core.property.WidgetSpecProperty
import org.json.JSONObject
import java.lang.reflect.Constructor

/**
 * 视图特殊属性解析类
 */
object WidgetSpecPropertyParser {

//    @JvmField
//    val map: MutableMap<String, Class<out WidgetSpecProperty>> = HashMap()
//
//    init {
//        //布局
//        map[WidgetTypeConst.LAYOUT_LINEAR_VERTICAL] = LinearLayoutProperty::class.java
//        map[WidgetTypeConst.LAYOUT_LINEAR_HORIZONTAL] =
//            LinearLayoutProperty::class.java
//        map[WidgetTypeConst.LAYOUT_FRAME] = FrameLayoutProperty::class.java
//        map[WidgetTypeConst.LAYOUT_TEXT] = TextWidgetProperty::class.java
//        map[WidgetTypeConst.LAYOUT_IMAGE] = ImageWidgetProperty::class.java
//        map[WidgetTypeConst.LAYOUT_PLACEHOLDER] =
//            PlaceHolderWidgetProperty::class.java
//
//        //通用业务
//        map[WidgetData.BANNER_SINGLE] = BannerSimpleProperty::class.java
//        map[WidgetData.BANNER_GRID] = BannerGridProperty::class.java
//        map[WidgetData.GRID_VERTICAL] = VerticalGridLayoutProperty::class.java
//        map[WidgetData.GRID_HORIZONTAL] =
//            HorizontalGridLayoutProperty::class.java
//        map[WidgetData.LIST] = ListLayoutProperty::class.java
//        map[WidgetData.FLIP] = FlipLayoutProperty::class.java
//
//        //特定业务
//    }

    @JvmStatic
    //转数据
    fun parse(widgetType: String, jo: JSONObject): WidgetSpecProperty? {
        val clazz: Class<out WidgetSpecProperty>? = ClassCachePool.getClassByWidgetType(
            ClassCachePool.WIDGET_PROPERTY,
            widgetType
        ) as? Class<out WidgetSpecProperty>
        var constructor: Constructor<out WidgetSpecProperty>? =
            clazz?.getConstructor(JSONObject::class.java)
        constructor?.newInstance(jo).let {
            return it
        }

        constructor = clazz?.getConstructor(JSONObject::class.java, String::class.java)

        return constructor?.newInstance(widgetType, jo)

    }
}